<template>
	<view class="box">
		<image src="https://bzhl-dev1.oss-cn-chengdu.aliyuncs.com/bzhl/miniapp/yst/logo.png" mode="" class="logo"></image>
		<u-tabs :list="tablist" @click="tabClick"></u-tabs>
		<view class="formBox">
			<view v-if="type == '0'">
				<u--form labelPosition="left" :model="formModel" :rules="rules1" ref="formModel">
					<u-form-item label="登录账号" labelWidth="100" prop="phone" borderBottom ref="item1">
						<u--input v-model="formModel.phone" border="none" placeholder="请输入手机号" maxlength="11"></u--input>
					</u-form-item>
					<u-form-item prop="verificationCode" borderBottom>
						<u-input placeholder="请输入验证码" border="none" v-model="formModel.verificationCode">
							<template slot="suffix">
								<u-code ref="uCode" @change="codeChange" startText="获取验证码" changeText="X秒重新获取"
									endText="重新获取验证码"></u-code>
								<u-button @tap="getCode" :text="tips" type="text" size="mini"
									:seconds="seconds"></u-button>
							</template>
						</u-input>
					</u-form-item>
					<u-form-item label="登录密码" labelWidth="100" prop="password" borderBottom ref="item1">
						<view class="flex">
							<u--input v-model="formModel.password" border="none" placeholder="密码为6-16位字母和数字"
								:type="isShowPassword?'password':'text'"></u--input>
							<u-icon :name="isShowPassword? 'eye-off' : 'eye'" @click="togglePassShow"></u-icon>
						</view>
					</u-form-item>
					<!-- <u-form-item label="验证码" labelWidth="100" prop="code" borderBottom ref="item1">
						<view class="flex alignC">
							<u--input v-model="formModel.code" border="none"></u--input>
							<view  @click="getUuid">
								<image :src="uuidImg" mode="" style="width: 140rpx;height: 65rpx;"></image>
							</view>
						</view>
					</u-form-item> -->
				</u--form>
			</view>
			<view v-else>
				<u--form labelPosition="left" :model="formModel1" :rules="rules2" ref="formModel1">
					<u-form-item label="登录账号" labelWidth="100" prop="username" borderBottom ref="item1">
						<u--input v-model="formModel1.username" border="none" placeholder="请输入6-10位字母和数字"></u--input>
					</u-form-item>
					<u-form-item label="登录密码" labelWidth="100" prop="password" borderBottom ref="item1">
						<view class="flex">
							<u--input v-model="formModel1.password" border="none" placeholder="密码为6-16位字母和数字"
								:type="isShowPassword?'password':'text'"></u--input>
							<u-icon :name="isShowPassword? 'eye-off' : 'eye'" @click="togglePassShow"></u-icon>
						</view>
					</u-form-item>
					<u-form-item label="确认密码" labelWidth="100" prop="confirmPassword" borderBottom ref="item1">
						<view class="flex">
							<u--input v-model="formModel1.confirmPassword" border="none" placeholder="密码为6-16位字母和数字"
								:type="isShowPassword2?'password':'text'"></u--input>
							<u-icon :name="isShowPassword2? 'eye-off' : 'eye'" @click="togglePassShow2"></u-icon>
						</view>
					</u-form-item>
					<!-- <u-form-item label="验证码" labelWidth="100" prop="code" borderBottom ref="item1">
						<view class="flex alignC">
							<u--input v-model="formModel1.code" border="none"></u--input>
							<view @click="getUuid">
								<image :src="uuidImg" mode="" style="width: 140rpx;height: 65rpx;"></image>
							</view>
						</view>
					</u-form-item> -->
				</u--form>
			</view>

			<view class="botText flex">
				<text style="color: #ccc;">已有账号？</text> <text @click="goLogin">去登录</text>
			</view>
		</view>

		<view class="bottom">
			<view style="margin-bottom: 20rpx;">
				<u-checkbox-group>
					<u-checkbox v-model="checked" shape="circle" @change="checkboxChange">
					</u-checkbox>
					<view class="" style="color: #ccc;font-size: 24rpx;">
						我已阅读并同意<text class="link" @click="toPage(`/pagesA/commonPage/index?type=5`)">《用户协议》</text>和
						<text class="link" @click="toPage(`/pagesA/commonPage/index?type=6`)">《隐私政策》</text>
					</view>
				</u-checkbox-group>
			</view>
			<u-button text="注册" color="#00B4C7" @click="handleRegister"></u-button>
		</view>
	</view>
</template>

<script>
	import {
		$http
	} from '@/utils/index.js'
	import {
		sendSms,
		register,
		createCaptcha
	} from '../../common/API/loginOrregister.js'
	export default {
		data() {
			return {
				isShowPassword: true,
				isShowPassword2: true,
				uuid: '',
				uuidImg: '',
				formModel: {
					miniappKey: 'yst',
					password: '',
					phone: '',
					registerType: '0', // 注册方式0手机验证码1账号密码
					userType: '0', // 身份0老板1医生2会员
					verificationCode: '',
					code: '',
					uuid: ''
				},
				formModel1: {
					miniappKey: 'yst',
					password: '',
					confirmPassword: '',
					registerType: '1', // 注册方式0手机验证码1账号密码
					userType: '0', // 身份0老板1医生2会员
					username: '',
					code: '',
					uuid: ''
				},
				tablist: [{
						name: '手机号注册',
						type: '0'
					},
					{
						name: '账号密码注册',
						type: '1'
					},
				],
				type: '0',
				rules1: {
					// 字段名称
					phone: [{
							required: true,
							message: '请输入手机号',
							trigger: ['change', 'blur'],
						},
						{
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								// console.log(value, uni.$u.test.mobile(value))
								return uni.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['change', 'blur'],
						}
					],
					verificationCode: {
						type: 'string',
						required: true,
						len: 4,
						message: '请填写4位验证码',
						trigger: ['blur']
					},
					password: [
						{
							required: true,
							message: '请输入密码',
							trigger: ['change', 'blur'],
						},
						{
							pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/,
							// 正则检验前先将值转为字符串
							transform(value) {
								return String(value);
							},
							message: '密码长度6-16位字母和数字'
						}
					]
				},
				rules2: {
					username: [
						{
							required: true,
							message: '请输入用户名',
							trigger: ['change', 'blur'],
						},
						{
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,10}$/
								return reg.test(value)
							},
							message: '请输入6-10位字母和数字',
							// 触发器可以同时用blur和change
							trigger: ['change', 'blur'],
						}
					],
					password: [
						{
							required: true,
							message: '请输入密码',
							trigger: ['change', 'blur'],
						},
						{
							pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/,
							// 正则检验前先将值转为字符串
							transform(value) {
								console.log(value,'mima')
								return String(value);
							},
							message: '密码为6-16位字母和数字'
						},
						// 自定义规则判断是否包含字母"A"
						{
							validator: (rule, value, callback) => {
								return /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/.test(value);
							},
							message: '密码为6-16位字母和数字'
						},
					],
					verificationCode: {
						type: 'string',
						required: true,
						len: 4,
						message: '请填写4位验证码',
						trigger: ['blur']
					},
					
				},
				checked: false, //同意条款
				seconds: 60,
				tips: '',
			}
		},
		async onShow() {
			this.getUuid()
		},
		methods: {
			togglePassShow(index) {
				this.isShowPassword = !this.isShowPassword
			},
			togglePassShow2(index) {
				this.isShowPassword2 = !this.isShowPassword2
			},
			// 获取uuid
			async getUuid() {
				const res = await createCaptcha()
				if (res.status == 200) {
					this.uuid = res.data.uuid
					this.uuidImg = `data:image/png;base64,${res.data.img}`
					// this.formModel.uuid = this.uuid
					// this.formModel1.uuid = this.uuid
				}
			},
			tabClick(item) {
				console.log('item', item);
				this.type = item.type
				this.formModel.registerType = item.type
			},
			codeChange(text) {
				// console.log(text)
				this.tips = text;
			},
			checkboxChange(val) {
				// console.log('change', n);
				this.checked = val
			},
			async getCode() {
				if (!this.formModel.phone) {
					uni.$u.toast('请先输入手机号');
					return;
				}
				if (this.$refs.uCode.canGetCode) {
					uni.showLoading({
						title: '正在获取验证码'
					})
					const res = await sendSms(this.formModel.phone)
					// console.log(res)
					if (res.status == 200) {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			end() {
				uni.$u.toast('倒计时结束');
			},
			start() {
				uni.$u.toast('倒计时开始');
			},
			goLogin() {
				uni.navigateTo({
					url: '/pages/login/login'
				})
			},
			handleRegister() {
				let data;
				if (this.type == 0) {
					data = this.formModel
				} else {
					if(!this.formModel1.username) {
						uni.$u.toast('注册账号不能为空');
						return false;
					}
					if(this.formModel1.username.length < 6) {
						uni.$u.toast('用户名为6-10位字母和数字');
						return;
					}
					// if(this.formModel1.password && /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/.test(this.formModel1.password)) {
					// 	uni.$u.toast('密码为6-16位字母和数字');
					// 	return;
					// }
					if(this.formModel1.confirmPassword != this.formModel1.password) {
						uni.$u.toast('密码不一致');
						return;
					}
					data = this.formModel1
				}
				
				if (data && !this.checked) {
					uni.$u.toast('请勾选用户协议')
					return;
				}
				console.log("注册",data)
				uni.request({
					url: `${$http.baseUrl}/api/auth/miniApp/register`, //请求路径
					data: JSON.stringify(data),
					method: "POST", //请求方式
					header: { //请求头配置
						'Content-Type': 'application/json', //设置请求类型，到接口文档中找
					},
					success: (res) => { //请求成功后的回调
						let data = res.data
						console.log("注册res",data)
						if (data.status == 200) {
							uni.$u.toast(data.message)
							setTimeout(() => {
								this.goLogin()
							}, 2000)
						}else {
							uni.$u.toast(data.message)
						}
					},
					fail: (err) => { //请求失败后的回调
						uni.showToast({
							icon: "error",
							title: err,
							duration: 1000
						})
					}
				})

			}
		}
	}
</script>

<style lang="scss" scoped>
	.box {
		width: 80%;
		margin: 220rpx auto;
		position: relative;
		height: calc(100vh - 220rpx);

		.logo {
			width: 276rpx;
			height: 80rpx;
			margin-bottom: 50rpx;
		}

		.formBox {
			margin-top: 50rpx;

			.botText {
				margin-top: 20rpx;
				justify-content: flex-end;
				font-size: 24rpx;
			}

			/deep/.u-button__text {
				color: #00B4C7 !important;
				font-size: 24rpx !important;
			}
		}

		.bottom {
			width: 100%;
			position: absolute;
			bottom: 100rpx;
			.link{
				color: #00B4C7;
			}
		}
	}
</style>